home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Toolbox / CustomIcon / Source / CustIcon.c
Encoding:
C/C++ Source or Header  |  1996-09-17  |  8.1 KB  |  341 lines  |  [TEXT/CWIE]

  1. // This snippet shows how to use custom document icons in an application.  
  2. // The correct procedure for doing this is to add the Icon family 
  3. // to the document and set bit 10 of the finder info.
  4. //
  5. // An elegant way of adding the icon family is to use one of the routines 
  6. // described in the Icon Utilities chapter of Inside Macintosh More Toolbox 
  7. // Essentials:   ForEachIconDo().  You can define  an action proc that is called 
  8. // each time for each icon an an icon suite.
  9. //
  10. // AppleLink: NICKT
  11. //
  12. //    Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  13.  
  14. //updates 8/96: CustIcon.h added as prefix file for 68K and PPC MC projects; old
  15. //routine names changed; void *myDataPtr removed from definition of routine MyIconAction;
  16. //FSSpec theFSSpec and short theRef removed from HandleMenuCommand; Rect screenRect and
  17. //GrafPtr oldPort removed from MainEventLoop; OSErr retCode, long gestResponse, EventRecord
  18. //Event and short count removed from InitToolbox; long lByteCnt removed from HandleKeyPress
  19.  
  20. #define    WWIDTH        470
  21. #define    WHEIGHT        330
  22.  
  23. #define WLEFT        (((screenBits.bounds.right - screenBits.bounds.left) - WWIDTH) / 2)
  24. #define WTOP        (((screenBits.bounds.bottom - screenBits.bounds.top) - WHEIGHT) / 2)
  25.  
  26. #define HiWrd(aLong)    (((aLong) >> 16) & 0xFFFF)
  27. #define LoWrd(aLong)    ((aLong) & 0xFFFF)
  28.  
  29. #include <Processes.h>
  30. #include <DiskInit.h>
  31. #include <Resources.h>
  32. #include <Menus.h>
  33. #include <Devices.h>
  34. #include <StandardFile.h>
  35. #include <Dialogs.h>
  36. #include <Fonts.h>
  37. #include <Icons.h>
  38. #include <Finder.h>
  39. #include <Script.h>
  40.  
  41. //------------------------------------------------------------------------------
  42. // enumerated types for the menus
  43.  
  44. enum {
  45.     mApple = 128,
  46.     mFile
  47. } ;
  48.  
  49. enum {
  50.     iAbout = 1
  51. } ;
  52. enum {
  53.     iNew = 1,
  54.     iIcon,
  55.     iUnused1,
  56.     iQuit = 4
  57. } ;
  58.  
  59. //------------------------------------------------------------------------------
  60.  
  61. static Boolean pQuitFlag = false ;
  62.  
  63. //------------------------------------------------------------------------------
  64. // function prototypes
  65.  
  66. void InitToolbox( void ) ;
  67. void MainEventLoop( void ) ;
  68. void HandleKeyPress(EventRecord *event) ; 
  69. void HandleMenuCommand(long menuResult) ;
  70. void AdjustMenus( void ) ;
  71. pascal OSErr    MyIconAction( ResType    theType,
  72.                               Handle    *theIcon);
  73. OSErr TouchDir(short vRefNum, long dirID) ;
  74.  
  75. //-----------------------------------------------------------------------------
  76.  
  77.  
  78. void HandleMenuCommand(long menuResult)
  79. {
  80.     short        menuID;
  81.     short        menuItem;
  82.     Str255        daName;
  83.     DialogPtr    theDialog ; 
  84.     short        itemHit ;
  85.     SFTypeList    myTypes = { '????' } ;
  86.     Handle        myIconSuite ;
  87.     OSErr        theErr ;
  88.     
  89.     short        savedResFile ;
  90.     short        theResFile ;
  91.     
  92.     FInfo        myFndrInfo ;
  93.         
  94.     StandardFileReply    theSFReply ;
  95.  
  96.     menuID = HiWrd(menuResult);
  97.     menuItem = LoWrd(menuResult);
  98.     switch ( menuID ) {
  99.         case mApple:
  100.             switch ( menuItem ) {
  101.                 case iAbout:
  102.                     theDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
  103.                     SetDialogDefaultItem(theDialog, 1) ;
  104.  
  105.                     do {
  106.                         ModalDialog ( nil, &itemHit );
  107.                     } while( itemHit != ok ) ;
  108.                     DisposeDialog ( theDialog );
  109.                     break;
  110.                     
  111.                 default:
  112.                     GetMenuItemText(GetMenuHandle(mApple), menuItem, daName);
  113.                     (void) OpenDeskAcc(daName);
  114.                     break;
  115.             }
  116.             break;
  117.         case mFile:
  118.             switch ( menuItem ) {
  119.                 case iNew:
  120.                     // create a file
  121.                     // put up a standard file put dialog to get the name
  122.                     StandardPutFile("\pCreate File", "\pUntitled", &theSFReply) ;
  123.                     if( theSFReply.sfGood ) {
  124.                         FSpCreateResFile( &theSFReply.sfFile, '????', 'TEXT', smSystemScript) ;
  125.                         theErr = ResError();
  126.                     }
  127.                     break ;
  128.                     
  129.                 case iIcon:
  130.                     // add icon family to file
  131.                     
  132.                     // put up std get file dlog
  133.                     StandardGetFile( nil, -1, myTypes, &theSFReply ) ;
  134.                     if( theSFReply.sfGood ) {
  135.                         
  136.                         // save the current resource file
  137.                         savedResFile = CurResFile();
  138.                         
  139.                         // Load the icon suite we want to set up
  140.                         theErr = GetIconSuite( &myIconSuite, 129, svAllAvailableData )  ;
  141.                         
  142.                         // get the finder info for the file
  143.                         theErr = FSpGetFInfo( &theSFReply.sfFile, &myFndrInfo ) ;
  144.                         
  145.                         // set bit 10 (has custom icon) and unset the inited flag
  146.                         // kHasBeenInited is 0x0100 so the mask will be 0xFEFF:
  147.                         
  148.                         myFndrInfo.fdFlags = 0xFEFF & (myFndrInfo.fdFlags | kHasCustomIcon ) ;
  149.                         
  150.                         // write it back
  151.                         theErr = FSpSetFInfo( &theSFReply.sfFile, &myFndrInfo ) ;
  152.                         
  153.                         // just check it already has a resource fork
  154.                         FSpCreateResFile( &theSFReply.sfFile, '????', 'TEXT', smSystemScript) ;
  155.                         theErr = ResError();
  156.                         
  157.                         // open the resfile the user picked
  158.                         theResFile = FSpOpenResFile ( &theSFReply.sfFile, fsWrPerm);
  159.                         theErr = ResError() ;
  160.                     
  161.                         UseResFile ( theResFile );
  162.                         theErr = ResError() ;
  163.                         
  164.                         theErr = ForEachIconDo( myIconSuite, svAllAvailableData, NewIconActionProc(MyIconAction), nil ) ;
  165.                         CloseResFile( theResFile );
  166.                         
  167.                         theErr = ResError() ;
  168.                         
  169.                         UseResFile ( savedResFile );
  170.                         
  171.                     }
  172.                     
  173.  
  174.                     break ;
  175.                 case iQuit:
  176.                 
  177.                     pQuitFlag = true;
  178.                     break;
  179.             }
  180.             break;
  181.             
  182.     }
  183.     HiliteMenu(0);        // Unhighlight whatever MenuSelect or MenuKey hilited
  184. }
  185.  
  186. //---------------------------------------------------------------------------
  187. // write out the icon to the currently set res file
  188.  
  189. pascal OSErr    MyIconAction( ResType    theType,
  190.                               Handle    *theIcon)
  191.                               //void        *myDataPtr )
  192. {
  193.  
  194.     OSErr    theErr = noErr ;
  195.     long    lByteCnt = GetHandleSize( *theIcon );
  196.  
  197.     // theIcon is already a resource.  If we try to do an AddResource, it will fail
  198.     // with error -194, to prevent this, call detach resource.  You will need
  199.     // to reload the suite if you want to use it after this.
  200.     
  201.     DetachResource( *theIcon ) ;
  202.     if(( theErr = ResError()) != noErr )
  203.         return theErr ;    
  204.         
  205.     // write out each resource we are passed by ForEachIconDo
  206.     AddResource ( *theIcon, theType, kCustomIconResource, "\pCustom Finder Icon" );
  207.  
  208.     // check we are OK
  209.     theErr = ResError() ;
  210.  
  211.     return theErr ;
  212.     
  213. }
  214.  
  215. //---------------------------------------------------------------------------
  216.  
  217. void MainEventLoop()
  218. {
  219.     EventRecord     event;
  220.     WindowPtr       window;
  221.     short           thePart;
  222.     Point            aPoint = {100, 100};
  223.  
  224.     while( !pQuitFlag )
  225.     {
  226.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  227.         {
  228.             AdjustMenus() ;
  229.  
  230.             switch (event.what) {
  231.                 case mouseDown:
  232.                 
  233.                     thePart = FindWindow( event.where, &window );
  234.                     
  235.                     switch( thePart ) {
  236.                         case inMenuBar: 
  237.                             HandleMenuCommand(MenuSelect(event.where));
  238.                             break;
  239.                         
  240.                         case inDrag:
  241.                             break ;
  242.                     
  243.                         case inContent:
  244.                             break ;
  245.                     
  246.                         case inGoAway:
  247.                             break ;
  248.                             
  249.                         default:
  250.                             break ;
  251.                     }
  252.                     break ;
  253.                             
  254.                         
  255.                 case updateEvt:
  256.                     break ;
  257.                     
  258.                 case keyDown:
  259.                 case autoKey:
  260.                     HandleKeyPress(&event);
  261.                     break;
  262.                     
  263.                 case diskEvt:
  264.                     if ( HiWrd(event.message) != noErr ) 
  265.                         (void) DIBadMount(aPoint, event.message);
  266.                     break;
  267.                     
  268.                 case osEvt:
  269.                 case activateEvt:
  270.                     break;
  271.  
  272.  
  273.             }
  274.         }
  275.     }
  276. }
  277.  
  278. //------------------------------------------------------------------------------
  279.  
  280. void HandleKeyPress(EventRecord *event)
  281. {
  282.     char    key;
  283.  
  284.     key = event->message & charCodeMask;
  285.     
  286.     // just check to see if we want to quit...
  287.     if ( event->modifiers & cmdKey ) {        /* Command key down? */
  288.         HandleMenuCommand(MenuKey(key));
  289.     } 
  290. }
  291.  
  292. //------------------------------------------------------------------------------
  293.  
  294. void AdjustMenus( void ) 
  295. {
  296.     // ha - we don't got no menus
  297. }
  298.  
  299. //------------------------------------------------------------------------------
  300.  
  301. void main()
  302. {
  303.     InitToolbox() ;
  304.     
  305.     MainEventLoop();
  306. }
  307.  
  308. //------------------------------------------------------------------------------
  309.  
  310. void InitToolbox()
  311. {
  312.     Handle        menuBar = nil;
  313.  
  314.     InitGraf((Ptr) &qd.thePort);
  315.     InitFonts();
  316.     InitWindows();
  317.     InitMenus();
  318.     TEInit();
  319.     InitDialogs(0L);
  320.     InitCursor();
  321.  
  322.     // initialize application globals
  323.     
  324.     pQuitFlag = false;
  325.     
  326.     
  327.     menuBar = GetNewMBar(128);                // Read menus into menu bar, MBAR res id is 128
  328.     
  329.     if ( menuBar == nil )
  330.          ExitToShell();                        // if we dont have it then quit - your app 
  331.                                              // needs a dialog here
  332.  
  333.     SetMenuBar(menuBar);                    // Install menus
  334.     DisposeHandle(menuBar);
  335.     
  336.     AppendResMenu(GetMenuHandle(mApple), 'DRVR');    // Add DA names to Apple menu, ID 128
  337.  
  338.     DrawMenuBar();
  339. }
  340.  
  341.